package com.ecloud.gardensterminal.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ecloud.gardensterminal.common.ResultEnum;
import com.ecloud.gardensterminal.common.exception.ServerException;
import com.ecloud.gardensterminal.controller.vo.GardensAreaDeviceVo;
import com.ecloud.gardensterminal.dao.GardensAreaDeviceMapper;
import com.ecloud.gardensterminal.dao.GardensAreaMapper;
import com.ecloud.gardensterminal.dao.GardensDeviceMapper;
import com.ecloud.gardensterminal.domain.GardensArea;
import com.ecloud.gardensterminal.domain.GardensAreaDevice;
import com.ecloud.gardensterminal.domain.GardensDevice;
import com.ecloud.gardensterminal.server.GardensAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Squbi
 */
@Service
public class GardensAreaServiceImpl implements GardensAreaService {
    @Autowired
    private GardensAreaMapper areaMapper;
    @Autowired
    private GardensAreaDeviceMapper areaDeviceMapper;
    @Autowired
    private GardensDeviceMapper deviceMapper;

    private static final String AREA_ID = "area_id";
    private static final String DEVICE_ID = "device_id";

    @Override
    public void newArea(String areaName, String areaDescription) {
        if (areaName.isEmpty()) {
            throw new ServerException(ResultEnum.ARGS_ERR, "区域名称不可为空");
        }
        areaMapper.insertNewly(areaName, areaDescription);
    }

    @Override
    public void deleteArea(int areaId) {
        areaMapper.delete(new QueryWrapper<GardensArea>().eq(AREA_ID, areaId));
        areaDeviceMapper.delete(new QueryWrapper<GardensAreaDevice>().eq(AREA_ID, areaId));
    }

    @Override
    public List<GardensArea> selectAllArea() {
        return areaMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public void updateArea(GardensArea area) {
        if (area == null) {
            throw new ServerException(ResultEnum.ARGS_ERR, "参数不可为空");
        }
        areaMapper.updateById(area);
    }

    //--

    @Override
    public void newMapping(int areaId, int deviceId) {
        GardensArea gardensArea = areaMapper.selectById(areaId);
        GardensDevice gardensDevice = deviceMapper.selectById(deviceId);
        if (gardensArea == null) {
            throw new ServerException(ResultEnum.ARGS_ERR, "区域不存在");
        } else if (gardensDevice == null) {
            throw new ServerException(ResultEnum.ARGS_ERR, "设备不存在");
        }
        areaDeviceMapper.insertNewly(areaId, deviceId);
    }

    @Override
    public void deleteMapping(int areaId, int deviceId) {
        areaDeviceMapper.delete(new QueryWrapper<GardensAreaDevice>().eq(AREA_ID, areaId).eq(DEVICE_ID, deviceId));
    }

    @Override
    public GardensAreaDeviceVo selectAreaMapping(int areaId) {
        GardensArea gardensArea = areaMapper.selectById(areaId);
        if (gardensArea == null) {
            throw new ServerException(ResultEnum.ARGS_ERR, "区域不存在");
        }
        List<GardensDevice> gardensDevices = areaDeviceMapper.selectAllByAreaId(areaId);
        return new GardensAreaDeviceVo(gardensArea, gardensDevices);
    }

}
